﻿#include "AddressBook.h"
#include "AddressBookDelegate.h"
#include "AddressBookModel.h"
#include <QHBoxLayout>
#include <QListView>
#include <QModelIndex>
#include <QStandardItem>
#include "AddressBookHelper.h"
#include <QVBoxLayout>
#include <QEvent>
#include <QLabel>
#include "ScrollBar.h"
#include <QToolButton>
#include <QScrollBar>
#include <QDebug>

AddressBook::AddressBook(QWidget *parent)
    : QWidget(parent)
{
    this->setStyleSheet("background-color:white;");
    this->setFixedSize(399,333);
    initUI();
}

AddressBook::~AddressBook()
{
}

void AddressBook::initUI()
{
    if(m_pView != nullptr)
    {
        return;
    }

    m_pView = new QListView(this);
    //m_pView.setcu
    m_pView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_pView->setStyleSheet("border:none;");
    m_pView->setSpacing(2);
    m_pView->setMouseTracking(true);
    m_pView->setItemDelegate(new AddressBookDelegate(m_pView, this));
    m_pModel = new AddressBookModel(this);
    m_pView->setModel(m_pModel);
    m_pView->verticalScrollBar()->setPageStep(1);
    m_pView->verticalScrollBar()->setSingleStep(1);
    m_pView->verticalScrollBar()->setTracking(true);
//    m_pModel->sort(0);
    connect(m_pView->verticalScrollBar(), &QScrollBar::valueChanged,
            this, &AddressBook::slotViewScrollBarValueChanged);

//    connect(m_pView, &QListView::currentChanged, [=](const QModelIndex&, const QModelIndex&){
//       qDebug() << "1111111111111";
//    });

    m_pScrollBar = new ScrollBar(this);
    bool bOk = connect(m_pScrollBar, &ScrollBar::sigCurrentIndexChanged, [=](const int& index){
       m_pView->setCurrentIndex(m_pModel->getFirstItemByIndex(index));
    });

    QHBoxLayout* pLayout = new QHBoxLayout;
    pLayout->setMargin(0);
    pLayout->addSpacing(0);
    pLayout->addWidget(m_pView);
    pLayout->addSpacing(32);
    pLayout->addWidget(m_pScrollBar);

    QToolButton* pBtn = new QToolButton(this);
    pBtn->setStyleSheet("background: #FFFFFF;border-radius: 4px;border: 1px solid #00B0AE;"
                        "font-size: 10px;font-family: \"Microsoft YaHei\";font-weight: 400;color: #00B0AE;");
    pBtn->setFixedSize(81, 27);
    pBtn->setText(QObject::tr("OK"));
    bOk = connect(pBtn, &QToolButton::clicked,
                       this, &AddressBook::slotOkBtnClicked);
    QHBoxLayout* pBtnLayout = new QHBoxLayout;
    pBtnLayout->setMargin(0);
    pBtnLayout->addStretch();
    pBtnLayout->addWidget(pBtn);
    pBtnLayout->addStretch();

    QVBoxLayout* pVlayout = new QVBoxLayout;
    pVlayout->setAlignment(Qt::AlignHCenter);
    pVlayout->setMargin(24);
    pVlayout->setSpacing(0);
    pVlayout->addLayout(pLayout);
    pVlayout->addLayout(pBtnLayout);
    this->setLayout(pVlayout);
}

void AddressBook::slotOkBtnClicked()
{

}

void AddressBook::slotViewScrollBarValueChanged(int value)
{
    if(m_pModel == nullptr)
    {
        return;
    }

    int iIndex = m_pModel->getIndexByScrollBarValue(value);
    m_pScrollBar->setCurrentIndex(iIndex);
}

